<?php

/**
 * @desc: Plugin for config application layout and script
 * 
 * @author: Tuyendn<tuyendn@vnext.vn>
 * @since: 2012/02/21
 * */
class Vlib_Plugin_Layout extends Zend_Controller_Plugin_Abstract {

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $_request) { 
        $modName = $_request->getModuleName();
       
        $appConf = Vlib_Config::getConfig();
        $modConf = Vlib_Module_Config::getConfig($modName);

        $layoutName = $appConf['web']['layout'] ? $appConf['web']['layout'] : 'layout';
        if ($modConf && @array_key_exists('web', $modConf)) {
            $layoutName = $modConf['web']['layout'] ? $modConf['web']['layout'] : 'layout';
        }

        // init view
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        if (null == $viewRenderer->view)
            $viewRenderer->initView();
        $view = $viewRenderer->view;

        $view->moduleName = $modName;

        // load jquery script file
        $jqueryVersion = $appConf['web']['jquery']['version'] ? $appConf['web']['jquery']['version'] : '1.7.1';
        $jqueryScript = $view->baseUrl() . '/js/jquery/jquery-' . $jqueryVersion . '.js';
        $jqueryFormScript = $view->baseUrl() . '/js/jquery/jquery.form.js';
        // load jquery ui script file
        $jqueryUIVersion = $appConf['web']['jquery']['ui']['version'] ? $appConf['web']['jquery']['ui']['version'] : '1.8.17';
        $jqueryUIScript = $view->baseUrl() . '/js/jquery/ui/' . $jqueryUIVersion . '/jquery-ui-' . $jqueryUIVersion . '.custom' . '.js';
        $jqueryDatePickerJaScript = $view->baseUrl() . '/js/jquery/ui/' . $jqueryUIVersion . '/i18n/jquery.ui.datepicker-ja.js';
        $jqueryDatePickerScript = $view->baseUrl() . '/js/jquery/ui/' . $jqueryUIVersion . '/jquery.ui.datepicker.js';
        $jqueryTimePickerAddonScript = $view->baseUrl() . '/js/jquery/ui/' . $jqueryUIVersion . '/jquery-ui-timepicker-addon.js';
        
       
        // HungNV: load jquery context menu
        $jsContextMenuScript = $view->baseUrl() . '/js/jquery/jquery.contextMenu.js';
        $cssContextMenuScript = $view->baseUrl() . '/css/jquery.contextMenu.css';
        
        // SonHH: add library tinymce
        $tinymce = $view->baseUrl() . '/js/tinymce/jscripts/tiny_mce/tiny_mce.js';
        $cookie = $view->baseUrl() . '/js/jquery/jquery.cookie.js';
        // load module script file
        $modScriptDir = WEB_ROOT . DS . 'templates' . DS . $modName . DS . 'js';
        $modScriptFiles = $this->getFiles($modScriptDir);

        $rootScript = "var ROOT_URL = '" . $view->baseUrl . "';";
        
        $view->headScript()->appendFile($jqueryScript)
                ->appendFile($jqueryFormScript)
                ->appendFile($jqueryUIScript)
                ->appendFile($jqueryDatePickerScript)
                ->appendFile($jqueryDatePickerJaScript)
                ->appendFile($jqueryTimePickerAddonScript)
                ->appendScript($rootScript)
                ->appendFile($jsContextMenuScript)
	    ->appendFile($tinymce)
	    ->appendFile($cookie);
        

        $view->headScript()->appendFile($view->baseUrl() . '/js/common.js');
        $view->headScript()->appendFile($view->baseUrl() . '/js/state.js');
        $view->headScript()->appendFile($view->baseUrl() . '/js/windowBlur.js');
        if ($modScriptFiles) {
            foreach ($modScriptFiles as $file) {
                $view->headScript()->appendFile($view->baseUrl() . '/templates/' . $modName . '/js/' . $file);
            }
        }

        $jqueryUiCss = $view->baseUrl() . '/js/jquery/ui/' . $jqueryUIVersion . '/css/smoothness/jquery-ui-' . $jqueryUIVersion . '.custom.css';
        $view->headLink()->appendStylesheet($jqueryUiCss)
                         ->appendStylesheet($cssContextMenuScript)
                         ->appendStylesheet($cssAttachmentScript);
        
        // load all the css file
        $styleDir = WEB_ROOT . DS . 'css';
        $appStyleFiles = $this->getFiles($styleDir);
        if ($appStyleFiles) {
            foreach ($appStyleFiles as $file) {
                $view->headLink()->appendStylesheet($view->baseUrl() . '/css/' . $file);
            }
        }

        // load all the module css file
        $modStyleDir = WEB_ROOT . DS . 'templates' . DS . $modName . DS . 'css';
        $modStyleFiles = $this->getFiles($modStyleDir);
        if ($modStyleFiles) {
            foreach ($modStyleFiles as $file) {
                $view->headLink()->appendStylesheet($view->baseUrl() . '/templates/' . $modName . '/css/' . $file);
            }
        }
        
        $memberLogic = new Vlib_Models_Table();
        //get menu list
        $modules = $memberLogic->getModulesByUser();

        if (count($modules) == 0) {
            $_request->setModuleName('default');
            $_request->setControllerName('error');
            $_request->setActionName('accessdenied');
            Zend_Auth::getInstance()->getStorage()->clear();
        }
        
        //get product list
        $aryCondition = array();
        $products = $memberLogic->getProduct($aryCondition);
        
        $view->products = $products;
        $view->modules = $modules;
        
        // initialize layout
        $layoutConf = array(
            'layoutPath' => WEB_ROOT . DS . 'templates' . DS . 'layouts'
        );
        Zend_Layout::startMvc($layoutConf);
        Zend_Layout::getMvcInstance()->setLayout($layoutName);
    }

    public function getFiles($dir) {
        $styleFiles = array();
        if (is_dir($dir)) {
            $iterators = new DirectoryIterator($dir);
            foreach ($iterators as $file) {
                if ($file->isDir() || $file->isDot())
                    continue;
                $name = $file->getFilename();
                if (preg_match('/^[^a-z]/i', $name) || ('CVS' == $name)
                        || ('.svn' == strtolower($name))) {
                    continue;
                }
                $styleFiles[] = $name;
            }
        }
        return $styleFiles;
    }

}